define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campus_course_management/index' + location.search,
                    add_url: 'campus_course_management/add',
                    edit_url: 'campus_course_management/edit',
                    del_url: 'campus_course_management/del',
                    //multi_url: 'campus_course_management/multi',
                    //import_url: 'campus_course_management/import',
                    table: 'campus_course_management',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'brand.name', title: __('Brand.name'), operate: 'LIKE'},
                        {field: 'courseseries.name', title: __('Courseseries.name'), operate: 'LIKE'},
                        {field: 'class_type', title: __('Class_type'), searchList: {"1":__('Class_type 1'),"2":__('Class_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'students_num', title: __('Students_num')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('隐藏'),"1":__('展示')}, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        choose_index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campus_course_management/index' + location.search,
                    add_url: 'campus_course_management/add',
                    edit_url: 'campus_course_management/edit',
                    del_url: 'campus_course_management/del',
                    //multi_url: 'campus_course_management/multi',
                    //import_url: 'campus_course_management/import',
                    table: 'campus_course_management',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'class_type', title: __('Class_type'), searchList: {"1":__('Class_type 1'),"2":__('Class_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            $(document).on('click', '.btn-callback', function () {
                Layer.alert(JSON.stringify(Table.api.selecteddata(table)));
                Fast.api.close(JSON.stringify(Table.api.selecteddata(table)));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.add_are_group(1);
            Controller.add_practice_group(1);
            $("#c-brand_id").on("change",function(){
                $('#c-course_series_id, #c-course_id,#c-ware_id,#c-practice_id').val('');
                $('#c-course_series_id, #c-course_id').selectPageClear();
            })
            $("#c-course_series_id").data("params", function (obj) {
                    return {custom: {brand_id: $("#c-brand_id").val()}
                };
            });
            $("#c-course_id").data("params", function (obj) {
                    return {custom: {brand_id: $("#c-brand_id").val()}
                };
            });
            //鼠标离开调用接口判断课程是否有练习和课件
            $("#c-course_id").mouseleave(function(){
                jQuery.post('/admin/course/judge_course', {
                    course_id: $("#c-course_id").val(),
                }, function(data) {
                    console.log(data)
                    if(data.code == 1){
                        var data = data.data;
                        if(data.is_ware > 0){
                            $("#add_ware_group").hide();
                            $("#add_ware_group_judge").show();
                            //这里到时候前端要改
                            $("#add_ware_group_judge").html(data.ware[0].name);
                            $("#is_ware").val(0);
                        }else{
                            $("#add_ware_group").show();
                            $("#add_ware_group_judge").hide();
                            //这里到时候前端要改
                            $("#add_ware_group_judge").html();
                            $("#is_ware").val(1);
                        }
                        if(data.practice_count > 0){
                            $("#add_practice_group").hide();
                            $("#add_practice_group_judge").show();
                            //这里到时候前端要改
                            $("#add_practice_group_judge").html(data.practice[0].name);
                            $("#is_practice").val(0);
                        }else{
                            $("#add_practice_group").show();
                            $("#add_practice_group_judge").hide();
                            //这里到时候前端要改
                            $("#add_practice_group_judge").html();
                            $("#is_practice").val(1);
                        }
                    }else{
                    }
                });
            });
            $("#c-ware_id").data("params", function (obj) {
                //$("#c-p_id").val('');
                return {
                    custom: {
                        course_id: $("#c-course_id").val(),
                        type: 1
                    }
                };
            });
            $("#c-practice_id").data("params", function (obj) {
                //$("#c-p_id").val('');
                return {
                    custom: {
                        course_id: $("#c-course_id").val(),
                        type: 2
                    }
                };
            });

            $(document).on('click','.class_arrangement' ,function (e) {
                console.log( $(this).data('id'))
                Layer.open({
                    type: 2,
                    content:'/admin/mall_parents_order/delivery_child?id='+$(this).data('id'),
                    area:['100%','100%'],
                    //content: Config.url
                })
            })
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-brand_id").on("change",function(){
                $('#c-course_series_id, #c-course_id,#c-ware_id,#c-practice_id').val('');
                $('#c-course_series_id, #c-course_id').selectPageClear();
            })
            $("#c-course_series_id").data("params", function (obj) {
                return {custom: {brand_id: $("#c-brand_id").val()}
                };
            });
            $("#c-course_id").data("params", function (obj) {
                return {custom: {brand_id: $("#c-brand_id").val()}
                };
            });
            //鼠标离开调用接口判断课程是否有练习和课件
            $("#c-course_id").mouseleave(function(){
                jQuery.post('/admin/course/judge_course', {
                    course_id: $("#c-course_id").val(),
                }, function(data) {
                    console.log(data)
                    if(data.code == 1){
                        var data = data.data;
                        if(data.is_ware > 0){
                            $("#add_ware_group").hide();
                            $("#add_ware_group_judge").show();
                            //这里到时候前端要改
                            $("#add_ware_group_judge").html(data.ware[0].name);
                            $("#is_ware").val(0);
                        }else{
                            $("#add_ware_group").show();
                            $("#add_ware_group_judge").hide();
                            //这里到时候前端要改
                            $("#add_ware_group_judge").html();
                            $("#is_ware").val(1);
                        }
                        console.log(data.practice_count)
                        if(data.practice_count > 0){
                            console.log(1111111111)
                            console.log(data.practice[0].name)
                            $("#add_practice_group").hide();
                            $("#add_practice_group_judge").show();
                            //这里到时候前端要改
                            $("#add_practice_group_judge").html(data.practice[0].name);
                            $("#is_practice").val(0);
                        }else{
                            console.log(222222222)
                            $("#add_practice_group").show();
                            $("#add_practice_group_judge").hide();
                            //这里到时候前端要改
                            $("#add_practice_group_judge").html();
                            $("#is_practice").val(1);
                        }
                        if(data.practice_count > 0){
                            $("#add_practice_group").hide();
                            $("#add_practice_group_judge").show();
                            //这里到时候前端要改
                            $("#add_practice_group_judge").html(data.practice[0].name);
                            $("#is_practice").val(0);
                        }else{
                            $("#add_practice_group").show();
                            $("#add_practice_group_judge").hide();
                            //这里到时候前端要改
                            $("#add_practice_group_judge").html();
                            $("#is_practice").val(1);
                        }
                    }else{
                    }
                });
            });
            $("#c-ware_id").data("params", function (obj) {
                //$("#c-p_id").val('');
                return {
                    custom: {
                        course_id: $("#c-course_id").val(),
                        type: 1
                    }
                };
            });
            $("#c-practice_id").data("params", function (obj) {
                //$("#c-p_id").val('');
                return {
                    custom: {
                        course_id: $("#c-course_id").val(),
                        type: 2
                    }
                };
            });
            var ware_index = Config.ware_index
            var practice_index = Config.practice_index
            Controller.add_are_group(ware_index);
            Controller.add_practice_group(practice_index);
            Controller.api.bindevent();
        },
        //新增数据
        add_are_group(add_index) {
            $(document).on('click', '.add_ware_btn', function () {
                var html = ' <div class="group_border">' +
                    '            <i class="fa fa-trash del_icon del_ware_icon"></i>' +
                    '            <input type="hidden" name="row[ware][' + add_index + '][id]" value="0">' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2"></label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <input id="c-ware_title' + add_index + '" data-rule="required" class="form-control" name="row[ware][' + add_index + '][name]" type="text" placeholder="' + __('Please Input Ware Name') + '">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2"></label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <div class="input-group">' +
                    '                        <input id="c-ware' + add_index + '" class="form-control" size="50" name="row[ware][' + add_index + '][file]" type="hidden">' +
                    '                        <div class="input-group-addon no-border no-padding">' +
                    '                            <span><button type="button" id="plupload-ware-audio" class="btn btn-danger plupload" data-input-id="c-ware' + add_index + '" data-mimetype="video/*"><i class="fa fa-upload"></i> 音频</button></span>' +
                    '                            <span><button type="button" id="plupload-ware-image" class="btn btn-danger plupload" data-input-id="c-ware' + add_index + '" data-mimetype="image/*"><i class="fa fa-upload"></i> 图片</button></span>' +
                    '                            <span><button type="button" id="plupload-ware-video" class="btn btn-danger plupload" data-input-id="c-ware' + add_index + '" data-mimetype="video/*"><i class="fa fa-upload"></i> 视频</button></span>' +
                    '                        </div>' +
                    '                        <span class="msg-box n-right" for="c-ware' + add_index + '"></span>' +
                    '                    </div>' +
                    '                </div>' +
                    '            </div>' +
                    '        </div>';
                $('.add_ware_group').append(html);
                add_index++;
            })
            //删除
            $(document).on('click', '.del_ware_icon', function () {
                //获取当前删除课件id
                var current_ware_id = $(this).next().val();

                //获取已删除的课件id
                var del_ware_id = $('.del_ware_id');
                if (del_ware_id.val()) {
                    var del_ware_ids = del_ware_id.val() + ',' + current_ware_id;
                    del_ware_id.val(del_ware_ids)
                } else {
                    del_ware_id.val(current_ware_id);
                }
                $(this).parent().remove();
            })
            $(document).on("click", "form .add_ware_btn", function (e, obj) {
                Form.events.plupload(obj);
            });
        },
        add_practice_group(add_index) {
            $(document).on('click', '.add_practice_btn', function () {
                var html = ' <div class="group_border">' +
                    '            <i class="fa fa-trash del_icon del_practice_icon"></i>' +
                    '            <input type="hidden" name="row[practice][' + add_index + '][id]" value="0">' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2"></label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <input id="c-practice_title' + add_index + '" data-rule="required" class="form-control" name="row[practice][' + add_index + '][name]" type="text" placeholder="' + __('Please Input Practice Name') + '">' +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="form-group">' +
                    '                <label class="control-label col-xs-12 col-sm-2"></label>' +
                    '                <div class="col-xs-12 col-sm-8">' +
                    '                    <div class="input-group">' +
                    '                        <input id="c-practice' + add_index + '" class="form-control" size="50" name="row[practice][' + add_index + '][file]" type="hidden">' +
                    '                        <div class="input-group-addon no-border no-padding">' +
                    '                            <span><button type="button" id="plupload-practice-audio" class="btn btn-danger plupload" data-input-id="c-practice' + add_index + '" data-mimetype="video/*"><i class="fa fa-upload"></i> 音频</button></span>' +
                    '                            <span><button type="button" id="plupload-practice-image" class="btn btn-danger plupload" data-input-id="c-practice' + add_index + '" data-mimetype="image/*"><i class="fa fa-upload"></i> 图片</button></span>' +
                    '                            <span><button type="button" id="plupload-practice-video" class="btn btn-danger plupload" data-input-id="c-practice' + add_index + '" data-mimetype="video/*"><i class="fa fa-upload"></i> 视频</button></span>' +
                    '                        </div>' +
                    '                        <span class="msg-box n-right" for="c-practice' + add_index + '"></span>' +
                    '                    </div>' +
                    '                </div>' +
                    '            </div>' +
                    '        </div>';
                $('.add_practice_group').append(html);
                add_index++;
            })
            //删除
            $(document).on('click', '.del_practice_icon', function () {
                //获取当前删除课件id
                var current_practice_id = $(this).next().val();
                //获取已删除的课件id
                var del_practice_id = $('.del_practice_id');
                if (del_practice_id.val()) {
                    var del_practice_ids = del_practice_id.val() + ',' + current_practice_id;
                    del_practice_id.val(del_practice_ids)
                } else {
                    del_practice_id.val(current_practice_id);
                }
                $(this).parent().remove();
            })
            $(document).on("click", "form .add_practice_btn", function (e, obj) {
                Form.events.plupload(obj);
            });
        },
        api: {
            bindevent: function () {
                $(document).on("change", "input[name='row[period_status]']", function(){
                    console.log($(this).val())
                    var period_status = $(this).val();
                    if(period_status == '1'){
                        $("#period_show").show();
                    }else if(period_status == '0'){
                        $("#period_show").hide();
                    }
                });

                /*添加按课时收费*/
                var h_rows_i = $("#c_hour_num").val();
                $('#new_hour').on('click',function(){
                    h_rows_i++;
                    $("#c_hour_num").val(h_rows_i);
                    var html = '<div class="col-xs-12 col-sm-12 row_div" style="margin-top:10px;">' +
                                    '<div class="col-xs-12 col-sm-2">' +
                                        '<input class="form-control" style="width:100px;" type="text" name="row[h_name_'+h_rows_i+']">' +
                                    '</div>' +
                                    '<div class="col-xs-12 col-sm-2">' +
                                        '<input class="form-control" style="width:100px;" type="text" name="row[h_num_'+h_rows_i+']">' +
                                    '</div>' +
                                    '<div class="col-xs-12 col-sm-2">' +
                                        '<input class="form-control" style="width:100px;" type="text" name="row[h_total_price_'+h_rows_i+']">' +
                                    '</div>' +
                                    '<div class="col-xs-12 col-sm-2">' +
                                        '<input class="form-control" style="width:100px;" type="text" name="row[h_price_'+h_rows_i+']">' +
                                    '</div>' +
                                    '<div class="col-xs-12 col-sm-1">' +
                                        '<span class="btn btn-danger span_del_btn del_h" id="h_del_'+h_rows_i+'">删除</span>' +
                                    '</div>' +
                                '</div>';
                    $('#hour .tab_div ').append(html);
                });
                $(document).on('click', '.del_h', function(){
                    $(this).parents('.row_div').remove();
                })

                $(document).on("change", "input[name='row[monthly_status]']", function(){
                    console.log($(this).val())
                    var monthly_status = $(this).val();
                    if(monthly_status == '1'){
                        $("#monthly_show").show();
                    }else if(monthly_status == '0'){
                        $("#monthly_show").hide();
                    }
                });
                /*添加按月收费*/
                var m_rows_i = $("#c_monthly_num").val();
                $('#new_monthly').on('click',function(){
                    m_rows_i++;
                    $("#c_monthly_num").val(m_rows_i);
                    var html = '<div class="col-xs-12 col-sm-12 row_div" style="margin-top:10px;">' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_name_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_num_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_total_price_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[m_price_'+m_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-1">' +
                        '<span class="btn btn-danger span_del_btn del_m" id="m_del_'+m_rows_i+'">删除</span>' +
                        '</div>' +
                        '</div>';
                    $('#monthly .tab_div ').append(html);
                });
                $(document).on('click', '.del_m', function(){
                    $(this).parents('.row_div').remove();
                })

                $(document).on("change", "input[name='row[per_diem_status]']", function(){
                    console.log($(this).val())
                    var per_diem_status = $(this).val();
                    if(per_diem_status == '1'){
                        $("#per_diem_show").show();
                    }else if(per_diem_status == '0'){
                        $("#per_diem_show").hide();
                    }
                });
                /*添加按天收费*/
                var pe_rows_i = $("#c_per_diem_num").val();
                $('#new_per_diem').on('click',function(){
                    pe_rows_i++;
                    $("#c_per_diem_num").val(pe_rows_i);
                    var html = '<div class="col-xs-12 col-sm-12 row_div" style="margin-top:10px;">' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_name_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_num_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_total_price_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[pe_price_'+pe_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-1">' +
                        '<span class="btn btn-danger span_del_btn del_pe" id="pe_del_'+pe_rows_i+'">删除</span>' +
                        '</div>' +
                        '</div>';
                    $('#per_diem .tab_div ').append(html);
                });
                $(document).on('click', '.del_pe', function(){
                    $(this).parents('.row_div').remove();
                })


                /*添加上课安排*/
                var c_rows_i = $("#c_class_arrangement_num").val();
                $('#new_class_arrangement').on('click',function(){
                    c_rows_i++;
                    $("#c_class_arrangement_num").val(c_rows_i);
                    var html = '<div class="col-xs-12 col-sm-12 row_div" style="margin-top:10px;">' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[c_name_'+c_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[c_first_'+c_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[c_second_'+c_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-2">' +
                        '<input class="form-control" style="width:100px;" type="text" name="row[c_third_'+c_rows_i+']">' +
                        '</div>' +
                        '<div class="col-xs-12 col-sm-1">' +
                        '<span class="btn btn-danger span_del_btn del_m" id="c_del_'+c_rows_i+'">删除</span>' +
                        '</div>' +
                        '</div>';
                    $('#class_arrangement .tab_div ').append(html);
                });
                $(document).on('click', '.del_m', function(){
                    $(this).parents('.row_div').remove();
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});